package com.lotter.api.controller;


import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.lotter.api.dal.DateEditor;
import com.lotter.api.dal.DoubleEditor;
import com.lotter.api.dal.IntegerEditor;
import com.lotter.api.dal.LongEditor;
import com.lotter.common.exception.LotterCusException;


/**
 * annotation: BaseController所有的业务Controller 继承与它 
 * @author zhangzechi
 * 2016年7月8日 下午3:03:12
 * version :1.0.0
 */

public abstract class BaseController {
	
	// 记录日志 
    protected final Logger logger = Logger.getLogger(getClass());
	

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		
		binder.registerCustomEditor(int.class, new IntegerEditor());
		binder.registerCustomEditor(long.class, new LongEditor());
		binder.registerCustomEditor(double.class, new DoubleEditor());
		binder.registerCustomEditor(Date.class, new DateEditor());
	    /**
         * 防止XSS攻击
         */
       // binder.registerCustomEditor(String.class, new StringEscapeEditor(true, false));
	}
	


	
	/**
	 * 执行成功  返回Map
	 * @param message  执行结果
	 * @return
	 */
	protected Map<String,Object> ajaxLotterSuccess(Map<String,Object> map) {
		if(map == null){
			map = new HashMap<String, Object>();
		}
		map.put("statusCode", 200);
		map.put("message", "执行成功");
		return map;
	}
	
	/**
	 * 执行成功  返回Map
	 * @param message  执行结果
	 * @return
	 */
	protected Map<String,Object> ajaxLotterSuccess() {
		Map<String,Object> map =  new HashMap<String, Object>();
		map.put("statusCode", 200);
		map.put("message", "执行成功");
		return map;
	}

	
	
	
	/**
	 * 业务
	 * @param message  执行结果
	 * @return
	 */
	protected Map<String,Object> ajaxLotterStage(String message) {
		Map<String,Object> map =  new HashMap<String, Object>();
		map.put("statusCode", 300);
		map.put("message", message);
		return map;
	}
	
	/**
	 * 业务
	 * @param message  执行结果
	 * @return
	 */
	protected Map<String,Object> ajaxLotterUser(String message) {
		Map<String,Object> map =  new HashMap<String, Object>();
		map.put("statusCode", 301);
		map.put("message", message);
		return map;
	}
	
	/**
	 * 服务器异常
	 * @param message  执行结果
	 * @return
	 */
	protected Map<String,Object> ajaxLotterServer(String message) {
		Map<String,Object> map =  new HashMap<String, Object>();
		map.put("statusCode", 500);
		map.put("message", message);
		return map;
	}
	
	/**
	 * 获取request
	 * 
	 * @return
	 */
	protected HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	/**
	 * 获取session
	 * 
	 * @return
	 */
	protected HttpSession getSession() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
	}
	
	
	public String getString(String name) {
		return getString(name, null);
	}

	public String getString(String name, String defaultValue) {
		String resultStr = getRequest().getParameter(name);
		if (resultStr == null || "".equals(resultStr) || "null".equals(resultStr) || "undefined".equals(resultStr)) {
			return defaultValue;
		} else {
			return resultStr;
		}
	}
	
	//得到 Integer 
	public Integer getInteger(String name) throws LotterCusException{
		return getInteger(name, null);
	}

	public int getInteger(String name, Integer defaultValue) throws LotterCusException {
		String resultStr = getRequest().getParameter(name);
		if (resultStr != null) {
			return Integer.parseInt(resultStr);
		}
		return defaultValue;
	}

	
	
	public BigDecimal getBigDecimal(String name) throws LotterCusException{
		return getBigDecimal(name, null);
	}

	//得到 BigDecimal 
	public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) throws LotterCusException{
		String resultStr = getRequest().getParameter(name);
		if (resultStr != null) {
		    return new BigDecimal(resultStr);
		}
		return defaultValue;
	}
	/**
	* @Title: clear 
	* @Description: 清空session信息 
	* @param @param request    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	protected static void clear(HttpServletRequest request){
		request.getSession().invalidate();
	}
	

	
}
